package server

import (
	"service-hello/api"
	"service-hello/internal/infrastructure/conf"
	"time"

	"github.com/go-kratos/kratos/v2/log"
	"github.com/go-kratos/kratos/v2/middleware/logging"
	"github.com/go-kratos/kratos/v2/middleware/recovery"
	"github.com/go-kratos/kratos/v2/transport/grpc"
)

// New new a grpc server.
func NewGRPCServer(c *conf.GRPC, logger log.Logger, svc api.TemplateServer) *grpc.Server {
	var opts = []grpc.ServerOption{
		grpc.Address(c.Address),
		grpc.Timeout(30 * time.Second),
		grpc.Logger(logger),
		grpc.Middleware(
			recovery.Recovery(recovery.WithLogger(logger)),
			logging.Server(logger),
		),
	}

	srv := grpc.NewServer(opts...)
	api.RegisterTemplateServer(srv, svc)
	return srv
}
